package edu.ccut.saturn.manager.bundles.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.IValidateInfo;
import edu.ccut.saturn.component.IValidateUseInfo;
import edu.ccut.saturn.component.SaturnDataManager;
import edu.ccut.saturn.component.ValidateManager;
import edu.ccut.saturn.component.impl.SaturnValidateInfo;
import edu.ccut.saturn.component.impl.SaturnValidateUseInfo;
import edu.ccut.saturn.component.xml.XMLHelper;
import edu.ccut.saturn.manager.bundles.core.Activator;

public class AddDataItemValidate extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		String fullKey = (String) parameterContext.getParameterValue(0);
		String saturnItemName = (String) parameterContext.getParameterValue(1);
		String validateInfoFullkey = (String) parameterContext
				.getParameterValue(2);

		List<String> eventList = new ArrayList<String>();
		if ((String) parameterContext.getParameterValue(3) != null) {
			eventList = Arrays.asList(((String) parameterContext
					.getParameterValue(3)).split(","));
		}

		List<String> argList = new ArrayList<String>();
		if ((String) parameterContext.getParameterValue(4) != null) {
			argList = Arrays.asList(((String) parameterContext
					.getParameterValue(4)).split(","));
		}

		String validateName = (String) parameterContext.getParameterValue(5);
		String description = (String) parameterContext.getParameterValue(6);
		String validateType = (String) parameterContext.getParameterValue(7);
		Map<String, String> errorMapInfos = this
				.getErrorMapInfos(parameterContext.getParameterValue(8));

		IDataInfo dataInfo = SaturnDataManager.getInstance().getSaturnDataInfo(
				fullKey);
		IDataItem dataItem = dataInfo.getDataItem(saturnItemName);
		IValidateInfo validateInfo = new SaturnValidateInfo(ValidateManager
				.getInstance().getValidateInfo(validateInfoFullkey));
		IValidateUseInfo validateUses = dataItem.getValidateUseInfo();

		if (validateUses == null) {
			List<IValidateInfo> validateInfoList = new ArrayList<IValidateInfo>();
			
			validateInfo.setName(validateName);
			validateInfo.setEventInfos(eventList);
			validateInfo.setArgs(argList);
			validateInfo.setKey(validateInfoFullkey);
			validateInfo.setDescription(description);
			validateInfo.setValidateType(validateType);
			validateInfo.setErrorInfos(errorMapInfos);
			
			validateInfoList.add(validateInfo);		
			IValidateUseInfo validateUseInfo = new SaturnValidateUseInfo(
					dataItem.getName(), dataItem.getJavaType(), true,
					validateInfoList);
			dataItem.setValidateUseInfo(validateUseInfo);
			
		} else {
			
			validateInfo.setName(validateName);
			validateInfo.setEventInfos(eventList);
			validateInfo.setArgs(argList);
			validateInfo.setKey(validateInfoFullkey);
			validateInfo.setDescription(description);
			validateInfo.setValidateType(validateType);
			validateInfo.setErrorInfos(errorMapInfos);
			
			validateUses.getValidateInfos().add(validateInfo);
		}
		this.createSaturnDataPachFileByDataInfo(fullKey, dataInfo);
	}
	
	private void createSaturnDataPachFileByDataInfo(String fullKey, IDataInfo dataInfo) throws IOException {

		// 将componentInfo生成文件
		String dir = Activator.getServerLocation() + File.separator + "patch"
				+ File.separator;
		// "c:" + File.separator + "patch" + File.separator;
		String[] fullKeyItem = fullKey.split("::");
		String directPath = (fullKeyItem[0].substring(7, fullKeyItem[0]
				.length())).replaceAll("/", File.separator + File.separator);

		String directPathTransformed = dir + directPath + File.separator;

		String componentFileDir = null;
		if (dataInfo.getSqlStr() != null && !"".equals(dataInfo.getSqlStr())) {
			componentFileDir = fullKeyItem[1] + ".sqldata";
		} else {
			componentFileDir = fullKeyItem[1] + ".saturndata";
		}
		XMLHelper.writeFile(directPathTransformed, componentFileDir, dataInfo
				.toXmlStr());
	}

	private Map<String, String> getErrorMapInfos(Object errorMapStr) {

		Map<String, String> errorMapInfos = new HashMap<String, String>();
		String[] errorMapArr = ((String) errorMapStr).split(",");

		for (int i = 0; i < errorMapArr.length; i++) {
			String str = errorMapArr[i];
			String key = str.substring(0, str.indexOf(":"));
			String value = str.substring(str.indexOf(":") + 1);
			errorMapInfos.put(key, value);
		}
		return errorMapInfos;
	}
}